package com.aaa.phone.controller.storecontroller;

import com.aaa.entity.AppSecondStore;
import com.aaa.phone.service.storeservice.StoreService;
import com.aaa.util.PictureUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.util.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.Map;

/**
 * @program: battery
 * @description: 用于手机端商城及二手市场获取数据库数据
 * @author: liangyongzhen
 * @create: 2018-12-19 20:36
 **/
@Controller
@CrossOrigin
@RequestMapping("/store")
public class StoreController {

    @Autowired
    private StoreService storeService;

    /**
     * @Description:获取所有电动车信息
     * @Param:
     * @return:
     * @Author: liangyongzhen
     * @Date: 2018/12/19
     */

    @RequestMapping("/getAllCarInfo")
    @ResponseBody
    public List<Map> getAllCarInfo(){

        List<Map> carList = storeService.getAllCarInfo();

        return carList;
    }

   

    /**
    * @Description: 获取一些电动车信息进行预览
    * @Param: []
    * @return: java.util.List<java.util.Map>
    * @Author: liangyongzhen
    * @Date: 2018/12/25
    */ 
    @RequestMapping("/getSomeInfo")
    @ResponseBody
    public List<Map> getSomeInfo(){

        List<Map> carList = storeService.getSomeInfo();
        return carList;
    }
    
    /**
     * @Description: 根据id来获取电动车信息
     * @Param: [id]
     * @return: java.util.List<java.util.Map>
     * @Author: liangyongzhen
     * @Date: 2018/12/20
     */
    @RequestMapping("/getCarInfoById")
    @ResponseBody
    public List<Map> getCarInfoById(Integer id){


        List<Map> carList = storeService.getCarInfoById(id);
        return carList;
    }

    /**
     * @Description:获取所有配件信息
     * @Param:
     * @return:
     * @Author: liangyongzhen
     * @Date: 2018/12/19
     */
    @RequestMapping("/getAllPartsInfo")
    @ResponseBody
    public List<Map> getAllPartsInfo(){

        List<Map> partsList = storeService.getAllPartsInfo();
        return partsList;
    }
    /**
     * @Description: 根据id来获取电动车配件信息
     * @Param: [id]
     * @return: java.util.List<java.util.Map>
     * @Author: liangyongzhen
     * @Date: 2018/12/20
     */
    @RequestMapping("/getPartInfoById")
    @ResponseBody
    public List<Map> getPartInfoById(Integer id){

        List<Map> partList = storeService.getPartInfoById(id);
        return partList;
    }


    /**
     * @Description: 添加二手车信息
     * @Param: [secondHandCar]
     * @return: int
     * @Author: liangyongzhen
     * @Date: 2018/12/21
     */
    @RequestMapping("/addSecondCarInfo")
    @ResponseBody
    public int addSecondCarInfo(AppSecondStore secondHandCar){
        storeService.addSecondCarInfo(secondHandCar);
        return 200;
    }

    /**
     * @Description: 查询二手车信息
     * @Param: []
     * @return: java.util.List<com.aaa.entity.AppSecondStore>
     * @Author: liangyongzhen
     * @Date: 2018/12/22
     */
    @RequestMapping("/getAllSecondCarInfo")
    @ResponseBody
    public List<AppSecondStore> getAllSecondCarInfo(){

        List<AppSecondStore> allSecondCarInfo = storeService.getAllSecondCarInfo();

        return allSecondCarInfo;
    }

    /**
     * @Description: 根据电话号码获取二手车信息，点击查看详情页面使用
     * @Param: [telephone]
     * @return: java.util.List<com.aaa.entity.AppSecondStore>
     * @Author: liangyongzhen
     * @Date: 2018/12/24
     */
    @RequestMapping("/getSecondCarInfoByTel")
    @ResponseBody
    public List<AppSecondStore> getSecondCarInfoByTel(String telephone){

        List<AppSecondStore> allSecondCarInfo = storeService.getSecondCarInfoByTel(telephone);

        return allSecondCarInfo;
    }
    /**
     * 上传头像
     * @param req
     * @param resp
     * @throws IOException
     */
    @RequestMapping("/updateImg")
    public void test(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String imageBase64 = req.getParameter("imageBase64");
        //将base64的图片编码存储至服务器，并将路径修改至数据库
        //前台是以png的格式存储的
        String type = "data:image/png;base64,";

        imageBase64 = imageBase64.substring(type.length());
        String realPath = "head_photo" + File.separator + "123" + ".png";
        JSONObject json = new JSONObject();
        try {
            File file = this.uploadFileByBase64(realPath, imageBase64);
            String s = PictureUtil.getPicture(file);

            json.put("result", "success");
            json.put("message", s);

        } catch (Exception e) {
            json.put("result", "error");
            json.put("message", "上传图片失败!");
            e.printStackTrace();
        }
        //将参数输出至浏览器
        PrintWriter out = resp.getWriter();
        out.write(json.toJSONString());
        out.flush();
        out.close();

    }


    public File uploadFileByBase64(String destRealPath, String imageBase64) throws Exception {
        OutputStream out = null;
        try {
            byte bytes[] = Base64.decodeFast(imageBase64);

            //这里是我的获取图片文件服务器的路径，可以是随便一个地址，如d:\\temp\\之类的
            String imageServer = "D:\\";
            String destFilePath = imageServer + File.separator + destRealPath;
            File destFile = new File(destFilePath);
            if (!destFile.getParentFile().exists()) {
                destFile.getParentFile().mkdirs();
            }
            for (int i = 0, lens = bytes.length; i < lens; i++) {
                if (bytes[i] < 0) {
                    bytes[i] += 256;
                }
            }
            out = new FileOutputStream(destFile);
            out.write(bytes);
            return destFile;
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            if (out != null) {
                try {
                    out.flush();
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
